package com.rmt.www.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sun.java2d.pipe.SpanClipRenderer;


import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rmt.www.constant.ArtifactType;
import com.rmt.www.constant.RequestType;
import com.rmt.www.dao.ArtifactDao;
import com.rmt.www.dao.ProjectDao;
import com.rmt.www.dao.RequestDao;
import com.rmt.www.dao.TraceabilityDao;
import com.rmt.www.dao.UserDao;
import com.rmt.www.daoImpl.RequestDaoImpl;
import com.rmt.www.service.AdminManageDetailService;
import com.rmt.www.vo.Artifact;
import com.rmt.www.vo.DesignArtifact;
import com.rmt.www.vo.DesignArtifactHistory;
import com.rmt.www.vo.DesignArtifactRequest;
import com.rmt.www.vo.FeatureArtifact;
import com.rmt.www.vo.FeatureArtifactRequest;
import com.rmt.www.vo.ImplementationArtifact;
import com.rmt.www.vo.ImplementationArtifactRequest;
import com.rmt.www.vo.Project;
import com.rmt.www.vo.Request;
import com.rmt.www.vo.RequirementArtifact;
import com.rmt.www.vo.RequirementArtifactRequest;
import com.rmt.www.vo.TestArtifact;
import com.rmt.www.vo.TestArtifactHistory;
import com.rmt.www.vo.TestArtifactRequest;
import com.rmt.www.vo.Traceability;
import com.rmt.www.vo.TraceabilityV2;
import com.rmt.www.vo.User;


public class RequestDetailAction extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int rid;
	private String artifacttype;
	int incEffort;
	private String effortChange;
	private String dependencies;
	private String priority;
	private String risk;
	private int pid;
	
	public int getIncEffort() {
		return incEffort;
	}

	public void setIncEffort(int incEffort) {
		this.incEffort = incEffort;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	private FeatureArtifact featureArtifact;
	private FeatureArtifactRequest featureArtifactRequest;
	private RequirementArtifact requirementArtifact;
	private RequirementArtifactRequest requirementArtifactRequest;
	private DesignArtifact designArtifact;
	private DesignArtifactRequest designArtifactRequest;
	private ImplementationArtifact implementationArtifact;
	private ImplementationArtifactRequest implementationArtifactRequest;
	private TestArtifact testArtifact;
	private TestArtifactRequest testArtifactRequest;
	private List<TraceabilityV2> traceabilityV2List;
	
	private AdminManageDetailService adminManageDetailService;
	public AdminManageDetailService getAdminManageDetailService() {
		return adminManageDetailService;
	}

	public void setAdminManageDetailService(
			AdminManageDetailService adminManageDetailService) {
		this.adminManageDetailService = adminManageDetailService;
	}


	


	public int getRid() {
		return rid;
	}

	public void setRid(int rid) {
		this.rid = rid;
	}
	
	public String execute(){
		List<TraceabilityV2> tList = new ArrayList<TraceabilityV2>();
		if (artifacttype.equalsIgnoreCase(ArtifactType.FEATURE)) {
			featureArtifactRequest = adminManageDetailService.getFeatureArtifactRequest(rid);
			priority = featureArtifactRequest.getPriority();
			risk = featureArtifactRequest.getRisk();
			if (featureArtifactRequest.getFeatureartifactid() != 0)
			{
				featureArtifact = adminManageDetailService.getFeatureArtifact(featureArtifactRequest.getFeatureartifactid());
				incEffort = featureArtifactRequest.getEffort() - featureArtifact.getEffort();
				tList = adminManageDetailService.getTraceabilityV2ByFrom("featureartifact", featureArtifactRequest.getFeatureartifactid());
			} else 
			{
				incEffort = featureArtifactRequest.getEffort();
			}
		} else if (artifacttype.equalsIgnoreCase(ArtifactType.REQUIREMENT)) {
			requirementArtifactRequest = adminManageDetailService.getRequirementArtifactRequest(rid);
			priority = requirementArtifactRequest.getPriority();
			risk = requirementArtifactRequest.getRisk();
			if (requirementArtifactRequest.getRequirementartifactid() != 0)
			{
				requirementArtifact = adminManageDetailService.getRequirementArtifact(requirementArtifactRequest.getRequirementartifactid());
				tList = adminManageDetailService.getTraceabilityV2ByFrom("requirementartifact", requirementArtifactRequest.getRequirementartifactid());
				incEffort = requirementArtifactRequest.getEffort() - requirementArtifact.getEffort();
			} 
			else 
			{
				incEffort = requirementArtifactRequest.getEffort();
			}
		} else if (artifacttype.equalsIgnoreCase(ArtifactType.DESIGN)) {
			designArtifactRequest = adminManageDetailService.getDesignArtifactRequest(rid);
			priority = designArtifactRequest.getPriority();
			risk = designArtifactRequest.getRisk();
			if (0 != designArtifactRequest.getDesignartifactid())
			{
				designArtifact = adminManageDetailService.getDesignArtifact(designArtifactRequest.getDesignartifactid());
				tList = adminManageDetailService.getTraceabilityV2ByFrom("designartifact", designArtifactRequest.getDesignartifactid());
				incEffort = designArtifactRequest.getEffort() - designArtifact.getEffort();
			}
			else 
			{
				incEffort = designArtifactRequest.getEffort();
			}
			
		} else if (artifacttype.equalsIgnoreCase(ArtifactType.IMPLEMENTATION)) {
			implementationArtifactRequest = adminManageDetailService.getImplementationArtifactRequest(rid);
			priority = implementationArtifactRequest.getPriority();
			risk = implementationArtifactRequest.getRisk();
			if (0 != implementationArtifactRequest.getImplartifactid())
			{
				implementationArtifact = adminManageDetailService.getImplementationArtifact(implementationArtifactRequest.getImplartifactid());
				tList = adminManageDetailService.getTraceabilityV2ByFrom("implartifact", implementationArtifactRequest.getImplartifactid());
				incEffort = implementationArtifactRequest.getEffort() - implementationArtifact.getEffort();
			}
			else 
			{
				incEffort = implementationArtifactRequest.getEffort();
			}
			
		} else if (artifacttype.equalsIgnoreCase(ArtifactType.TESTCASE)) {
			testArtifactRequest = adminManageDetailService.getTestArtifactRequest(rid);
			priority = testArtifactRequest.getPriority();
			risk = testArtifactRequest.getRisk();
			if (0 != testArtifactRequest.getTestartifactid())
			{
				testArtifact = adminManageDetailService.getTestArtifact(testArtifactRequest.getTestartifactid());
				tList = adminManageDetailService.getTraceabilityV2ByFrom("testartifact", testArtifactRequest.getTestartifactid());
				incEffort = testArtifactRequest.getEffort() - testArtifact.getEffort();
			}
			else 
			{
				incEffort = testArtifactRequest.getEffort();
			}
		}

		if (incEffort > 0) {
			effortChange = "INCREASE " + incEffort;
		} else if (incEffort == 0) {
			effortChange = "NO CHANGE";
		} else {
			effortChange = "DECREASE " + incEffort;
		}
		
		dependencies = "" + tList.size(); 
		if (tList.size() == 0) {
			dependencies = "NO artifact ";
		} else if (tList.size()== 1) {
			dependencies = "1 artifact ";
		} else {
			dependencies = tList.size() + " artifacts ";
		}
	
		return "success";
	}

	public String getArtifacttype() {
		return artifacttype;
	}

	public void setArtifacttype(String artifacttype) {
		this.artifacttype = artifacttype;
	}

	public FeatureArtifact getFeatureArtifact() {
		return featureArtifact;
	}

	public void setFeatureArtifact(FeatureArtifact featureArtifact) {
		this.featureArtifact = featureArtifact;
	}

	public FeatureArtifactRequest getFeatureArtifactRequest() {
		return featureArtifactRequest;
	}

	public void setFeatureArtifactRequest(
			FeatureArtifactRequest featureArtifactRequest) {
		this.featureArtifactRequest = featureArtifactRequest;
	}

	public RequirementArtifact getRequirementArtifact() {
		return requirementArtifact;
	}

	public void setRequirementArtifact(RequirementArtifact requirementArtifact) {
		this.requirementArtifact = requirementArtifact;
	}

	public RequirementArtifactRequest getRequirementArtifactRequest() {
		return requirementArtifactRequest;
	}

	public void setRequirementArtifactRequest(
			RequirementArtifactRequest requirementArtifactRequest) {
		this.requirementArtifactRequest = requirementArtifactRequest;
	}

	public DesignArtifact getDesignArtifact() {
		return designArtifact;
	}

	public void setDesignArtifact(DesignArtifact designArtifact) {
		this.designArtifact = designArtifact;
	}

	public ImplementationArtifact getImplementationArtifact() {
		return implementationArtifact;
	}

	public void setImplementationArtifact(
			ImplementationArtifact implementationArtifact) {
		this.implementationArtifact = implementationArtifact;
	}

	public ImplementationArtifactRequest getImplementationArtifactRequest() {
		return implementationArtifactRequest;
	}

	public void setImplementationArtifactRequest(
			ImplementationArtifactRequest implementationArtifactRequest) {
		this.implementationArtifactRequest = implementationArtifactRequest;
	}

	public TestArtifact getTestArtifact() {
		return testArtifact;
	}

	public void setTestArtifact(TestArtifact testArtifact) {
		this.testArtifact = testArtifact;
	}

	public List<TraceabilityV2> getTraceabilityV2List() {
		return traceabilityV2List;
	}

	public void setTraceabilityV2List(List<TraceabilityV2> traceabilityV2List) {
		this.traceabilityV2List = traceabilityV2List;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public DesignArtifactRequest getDesignArtifactRequest() {
		return designArtifactRequest;
	}

	public void setDesignArtifactRequest(DesignArtifactRequest designArtifactRequest) {
		this.designArtifactRequest = designArtifactRequest;
	}

	public TestArtifactRequest getTestArtifactRequest() {
		return testArtifactRequest;
	}

	public void setTestArtifactRequest(TestArtifactRequest testArtifactRequest) {
		this.testArtifactRequest = testArtifactRequest;
	}

	public String getEffortChange() {
		return effortChange;
	}

	public void setEffortChange(String effortChange) {
		this.effortChange = effortChange;
	}

	public String getDependencies() {
		return dependencies;
	}

	public void setDependencies(String dependencies) {
		this.dependencies = dependencies;
	}

	public String getPriority() {
		return priority;
	}

	public void setPriority(String priority) {
		this.priority = priority;
	}

	public String getRisk() {
		return risk;
	}

	public void setRisk(String risk) {
		this.risk = risk;
	}
}
